import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.GridBagLayout;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Color;


public class ServerWindow extends JFrame implements Runnable{
	Thread serverSession = new Thread(this);
	private JPanel contentPane;
	public static boolean isRunning = false;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ServerWindow frame = new ServerWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ServerWindow() {
		setTitle("Tower Defence Server");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 320, 85);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnStart = new JButton("START");
		
		btnStart.setBounds(109, 11, 89, 23);
		contentPane.add(btnStart);
		
		JButton btnStop = new JButton("STOP");
		btnStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				serverSession.stop();
				isRunning = false;
				Server.closeServer = true;
				System.exit(0);
			}
		});
		btnStop.setBounds(208, 11, 89, 23);
		contentPane.add(btnStop);
		
		JLabel lblStatus = new JLabel("Status:");
		lblStatus.setBounds(10, 15, 46, 14);
		contentPane.add(lblStatus);
		
		final JLabel lblStop = new JLabel("STOP");
		lblStop.setForeground(Color.RED);
		lblStop.setBounds(52, 15, 46, 14);
		contentPane.add(lblStop);
		
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				serverSession.start();
				lblStop.setForeground(Color.GREEN);
				lblStop.setText("START");
				///Server server = new Server();
			}
		});
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			if (!isRunning) {
				Server server = new Server();
				isRunning = true;
			}
			
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
